<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="systemDictionaryTypes" highlight-current-row v-loading="listLoading"
			@selection-change="selsChange" style="width: 100%;" @row-dblclick="getDetail">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="数据字典名">
			</el-table-column>
			<el-table-column prop="sn" label="编号">
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length === 0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
				:current-page.sync="currentPage" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="数据字典名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="编号" prop="sn">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!-- 抽屉 -->
		<el-drawer title="数据字典明细" :visible.sync="table" direction="rtl" size="50%">
			<el-table :data="systemDictionaryDetails" highlight-current-row v-loading="detailListLoading"
				@selection-change="detailSelsChange">
				<el-table-column type="selection" width="55">
				</el-table-column>
				<el-table-column type="index" label="编号" width="60">
				</el-table-column>
				<el-table-column property="name" label="明细名"></el-table-column>
				<el-table-column label="操作" width="150">
					<template slot="header">
						<el-button type="primary" @click="handleAddDetail">新增</el-button>
					</template>
					<template scope="scope">
						<el-button size="small" @click="handleEditDetail(scope.$index, scope.row)">编辑</el-button>
						<el-button type="danger" size="small" @click="handleDelDetail(scope.$index, scope.row)">删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>

			<!--工具条-->
			<el-col :span="24" class="toolbar">
				<el-button type="danger" @click="detailbatchRemove" :disabled="this.detailSels.length === 0">批量删除</el-button>
				<el-pagination layout="prev, pager, next" @current-change="detailhandleCurrentChange"
					:current-page.sync="detailcurrentPage" :page-size="detailpageSize" :total="detailtotal" style="float:right;">
				</el-pagination>
			</el-col>
		</el-drawer>

		<!-- 数据明细编辑界面 -->
		<el-dialog :title="detailedTitle" :visible.sync="editDetailFormVisible" :close-on-click-modal="false">
			<el-form :model="editDetailForm" label-width="80px" :rules="editDetailFormRules" ref="editDetailForm">
				<el-form-item label="明细名" prop="name">
					<el-input v-model="editDetailForm.name" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editDetailFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="detaileditSubmit" :loading="editDetailLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
import { getSystemDictionaryTypes, editSystemDictionaryType, removeSystemDictionaryType, batchSystemDictionaryTypes, getSystemDictionaryDetails,editSystemDictionaryDetail,removeSystemDictionaryDetail,batchSystemDictionaryDetail } from "../../api/api";
import util from '../../common/js/util'

export default {
	data() {
		return {
			// 查询
			filters: {
				keyword: ''
			},

			// 列表数据
			systemDictionaryTypes: [],

			// 分页
			total: 0,
			pageSize: 5,
			currentPage: 1,
			listLoading: false,
			sels: [],//列表选中列

			// 编辑
			editFormVisible: false,//编辑界面是否显示
			editLoading: false,
			editFormRules: {
				name: [
					{ required: true, message: '请输入数据字典名', trigger: 'blur' }
				],
				sn: [
					{ required: true, message: '请输入编号', trigger: 'blur' }
				],

			},

			//编辑界面数据
			editForm: {},

			// 部门负责人
			managers: [],

			// 选择上级部门
			parents: [],

			title: '添加',

			// 数据字典id
			types_id: null,

			// 数据字典明细字段
			// 抽屉显示/隐藏
			table: false,
			// 数据字典明细
			SystemDictionaryDetails: [],
			// 控制数据明细编辑界面
			editDetailFormVisible: false,
			editDetailLoading: false,
			editDetailFormRules: {
				name: [
					{ required: true, message: '请输入明细名', trigger: 'blur' }
				],
			},
			editDetailForm: {},
			detailedTitle: '',
			systemDictionaryDetails: [],
			detailListLoading: false,
			detailSels: [],
			detailtotal: 0,
			detailpageSize: 5,
			detailcurrentPage: 1,
		}
	},

	methods: {
		// 明细批量删除
		detailbatchRemove: function () {
			let ids = this.detailSels.map(item => item.id);
			this.$confirm('确认删除选中记录吗？', '提示', {
				type: 'warning'
			}).then(() => {
				this.detailListLoading = true;
				batchSystemDictionaryDetail(ids)
					.then((res) => {
						this.detailListLoading = false;
						if (res.data.success) {
							this.$message({
								message: res.data.message,
								type: 'success'
							});
						} else {
							this.$message({
								message: res.data.message,
								type: 'error'
							});
						}
						this.detailcurrentPage = 1;
						this.getDetailList();
					})
					.catch((error) => {
						this.detailListLoading = false;
						this.$message({
							message: '系统繁忙,请稍后!',
							type: 'error'
						});
					});

			}).catch(() => {

			});
		},

		// 删除明细
		handleDelDetail: function (index, row) {
			this.$confirm('确认删除该记录吗?', '提示', {
				type: 'warning'
			}).then(() => {
				this.detailListLoading = true;
				removeSystemDictionaryDetail(row.id)
					.then((result) => {
						this.detailListLoading = false;
						if (result.data.success) {
							this.$message({
								message: result.data.message,
								type: 'success'
							});
						} else {
							this.$message({
								message: result.data.message,
								type: 'error'
							});
						}
						this.detailcurrentPage = 1;
						this.getDetailList();
					}).catch((error) => {
						this.detailListLoading = false;
						this.$message({
							message: '系统繁忙,请稍后!',
							type: 'error'
						});
					})
			}).catch(() => {

			});
		},
		// 修改明细
		handleEditDetail: function (index, row) {
			this.title = '编辑明细';
			this.editDetailFormVisible = true;
			this.editDetailForm = Object.assign({}, row);
		},
		//明细编辑
		detaileditSubmit: function () {
			this.$refs.editDetailForm.validate((valid) => { // 数据验证
				if (valid) {
					this.$confirm('确认提交吗？', '提示', {}).then(() => {
						this.editDetailLoading = true;
						let para = Object.assign({}, this.editDetailForm);
						editSystemDictionaryDetail(para).then((res) => {
							if (res.data.success) {
								this.$message({
									message: res.data.message,
									type: 'success'
								});
								this.$refs['editDetailForm'].resetFields();
								this.editDetailFormVisible = false;
								this.editDetailLoading = false;
								if (this.title == '添加') {
									let total = this.detailtotal + 1;
									if (total % this.pageSize != 0) {
										this.detailcurrentPage = parseInt(total / this.detailpageSize) + 1;
									} else {
										this.detailcurrentPage = total / this.detailpageSize;
									}
								}
								this.getDetailList();
							} else {
								this.$message({
									message: res.data.message,
									type: 'error'
								});
							}
						}).catch((error) => {
							this.$message({
								message: '系统繁忙，请稍后再试!d',
								type: 'error'
							});
						});
					});
				}
			});
		},
		// 明细分页
		detailhandleCurrentChange(val) {
			console.log(val);
			this.currentPage = val;
			this.getDetailList();
		},
		// 数据字典明细选择
		detailSelsChange: function (sels) {
			this.detailSels = sels;
		},
		// 添加数据字典明细
		handleAddDetail() {
			this.detailedTitle = '添加明细';
			this.editDetailFormVisible = true;
			this.editDetailForm = {
				id: null,
				name: '',
				types_id: this.types_id
			}
		},
		// 显示数据字典明细
		getDetail(row) {
			this.table = true;
			this.detailcurrentPage = 1;
			this.types_id = row.id;
			this.getDetailList();
		},
		// 获取数据字典明细
		getDetailList() {
			let para = {
				types_id: this.types_id,
				pageSize: this.detailpageSize,
				currentPage: this.detailcurrentPage,
			};
			getSystemDictionaryDetails(para).then((res) => {
				this.detailtotal = res.data.total;
				this.systemDictionaryDetails = res.data.rows;
				this.detailListLoading = false;
			});
		},



		// 查询
		search() {
			this.currentPage = 1;
			this.List();
		},
		// 分页
		handleCurrentChange(val) {
			console.log(val);
			this.currentPage = val;
			this.List();
		},
		//获取部门列表
		List() {
			let para = {
				pageSize: this.pageSize,
				currentPage: this.currentPage,
				keyword: this.filters.keyword
			};
			// 开启忙等
			this.listLoading = true;
			//NProgress.start();
			getSystemDictionaryTypes(para).then((res) => {
				this.total = res.data.total;
				this.systemDictionaryTypes = res.data.rows;
				this.listLoading = false;
			});
		},

		//删除
		handleDel: function (index, row) {
			this.$confirm('确认删除该记录吗?', '提示', {
				type: 'warning'
			}).then(() => {
				this.listLoading = true;
				removeSystemDictionaryType(row.id)
					.then((result) => {
						this.listLoading = false;
						if (result.data.success) {
							this.$message({
								message: result.data.message,
								type: 'success'
							});
						} else {
							this.$message({
								message: result.data.message,
								type: 'error'
							});
						}
						this.currentPage = 1;
						this.List();
					}).catch((error) => {
						this.listLoading = false;
						this.$message({
							message: '系统繁忙,请稍后!',
							type: 'error'
						});
					})
			}).catch(() => {

			});
		},

		//显示编辑界面
		handleEdit: function (index, row) {
			this.title = '编辑';
			this.editFormVisible = true;
			this.editForm = Object.assign({}, row);
		},

		//显示新增界面
		handleAdd: function () {
			this.title = '添加';
			this.editFormVisible = true;
			this.editForm = {
				id: null,
				name: '',
				sn: '',
			};
		},

		//编辑
		editSubmit: function () {
			this.$refs.editForm.validate((valid) => { // 数据验证
				if (valid) {
					this.$confirm('确认提交吗？', '提示', {}).then(() => {
						this.editLoading = true;
						let para = Object.assign({}, this.editForm);
						editSystemDictionaryType(para).then((res) => {
							if (res.data.success) {
								this.$message({
									message: res.data.message,
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.editLoading = false;
								if (this.title == '添加') {
									let total = this.total + 1;
									if (total % this.pageSize != 0) {
										this.currentPage = parseInt(total / this.pageSize) + 1;
									} else {
										this.currentPage = total / this.pageSize;
									}
								}
								this.List();
							} else {
								this.$message({
									message: res.data.message,
									type: 'error'
								});
							}

						}).catch((error) => {
							this.$message({
								message: '系统繁忙，请稍后再试!d',
								type: 'error'
							});
						});
					});
				}
			});
		},

		// 数据选择
		selsChange: function (sels) {
			this.sels = sels;
		},

		//批量删除
		batchRemove: function () {
			var ids = this.sels.map(item => item.id);
			this.$confirm('确认删除选中记录吗？', '提示', {
				type: 'warning'
			}).then(() => {
				this.listLoading = true;
				batchSystemDictionaryTypes(ids)
					.then((res) => {
						this.listLoading = false;
						if (res.data.success) {
							this.$message({
								message: res.data.message,
								type: 'success'
							});
						} else {
							this.$message({
								message: res.data.message,
								type: 'error'
							});
						}
						this.currentPage = 1;
						this.List();
					})
					.catch((error) => {
						this.listLoading = false;
						this.$message({
							message: '系统繁忙,请稍后!',
							type: 'error'
						});
					});

			}).catch(() => {

			});
		}
	},
	mounted() {
		this.List();

	},

}

</script>

<style scoped>
</style>